package com.css.openapi.config;

import com.css.openapi.filter.SecurityFilter;
import com.css.openapi.interceptor.ApiAccessInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.servlet.Filter;


@Configuration
public class FilterConfiguration extends WebMvcConfigurationSupport {
    @Bean
    public FilterRegistrationBean<SecurityFilter> znsbSessionFilter() {
        return createFilterBean(new SecurityFilter(), -98);
    }
    public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
        FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
        bean.setOrder(order);
        return bean;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiAccessInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public HandlerInterceptor apiAccessInterceptor () {
        return new ApiAccessInterceptor();
    }


}
